clear;
%===============================================
data = load('ex1data1.txt');
X = data(:, 1);
y = data(:, 2);
m = length(y);

plotData(X, y);
% Pause();

%===============================================
X = [ones(m, 1), X];
theta = zeros(2, 1);
iterations = 1500;
alpha = 0.01;

J = computeCost(X, y, theta);
fprintf('With theta = [0 ; 0]\nCost computed = %f\n', J);
fprintf('Expected cost value (approx) 32.07\n');

J = computeCost(X, y, [-1; 2]);
fprintf('\nWith theta = [-1 ; 2]\nCost computed = %f\n', J);
fprintf('Expected cost value (approx) 54.24\n');

%===============================================
fprintf('\nRunning Gradient Descent ...\n');
[theta, J_history, theta_history] = gradientDescent(X, y, theta, alpha, iterations);
% fprintf('size of theta_history\n');
% s = size(theta_history)
fprintf('Theta found by gradient descent:\n');
fprintf('%f\n', theta);
fprintf('Expected theta values (approx)\n');
fprintf(' -3.6303\n  1.1664\n\n');
J = computeCost(X, y, theta);
fprintf('final cost value = %f\n', J);
hold on;
plot(X(:, 2), X * theta, '-');
legend('Training data', 'Linear regression')
hold off;
figure; plot(1:numel(J_history), J_history);

%===============================================

theta0_vals = linspace(-10, 10, 100);
theta1_vals = linspace(-1, 4, 100);
j_vals = zeros(length(theta0_vals), length(theta1_vals));

for i = 1:length(theta0_vals)

    for j = 1:length(theta1_vals)
        t = [theta0_vals(i); theta1_vals(j)];
        j_vals(i, j) = computeCost(X, y, t);
    end

end

figure;
surf(theta0_vals, theta1_vals, j_vals');
xlabel('theta_0'); ylabel('theta_1');

figure;
contour(theta0_vals, theta1_vals, j_vals', logspace(0, 3, 20));
xlabel('theta_0'); ylabel('theta_1');
hold on;
plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2); 
hold on;
plot(theta_history(1, :), theta_history(2, :), 'rx');
